<template>
    <div id="login">
        <div class="login_box">
            <!-- 头像区域 -->
            <div class="login_admin_head">
                <img src="../assets/images/3.jpg" alt="">
            </div>
            <!-- 登录表单区域 -->
            <el-form ref="login_form_ref" :model="login_form" :rules="login_form_rules" label-width="0px" class="login_form">
                <!-- 账号 -->
                <el-form-item prop="username">
                  <el-input v-model="login_form.username" prefix-icon="iconfont icon-user"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                  <el-input v-model="login_form.password" type="password" prefix-icon="iconfont icon-password"></el-input>
                </el-form-item>
                <!-- 按钮 -->
                  <el-form-item class="btns">
                   <el-button type="primary" @click="login">登录</el-button>
                   <el-button @click="login_form_reset">重置</el-button>
                 </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return {
            // 登陆表单数据绑定对象
            login_form: {
                username: "",
                password: ""
            },
            // 表单验证规则对象
            login_form_rules: {
                // 验证用户名是否合法
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                // 验证密码是否合法
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                ]
            },
        }
    },
    methods: {
        // 点击重置按钮，重置表单
        login_form_reset(){
            this.$refs.login_form_ref.resetFields()
        },
        login(){
            this.$refs.login_form_ref.validate(async valid => {
                if(!valid) return;
                let {data: res} = await this.axios.post("login",this.login_form)
                if(res.meta.status !== 200) return this.message.error("登陆失败，请检查用户名或密码是否输入正确")
                this.message.success("登陆成功")
                // console.log(res)
                /* 
                    1. 将登陆成功在之后的token，保存到客户端的sessionStorage中
                        1.1 项目中除了登陆之外的其他API接口，必须在登录之后才能访问
                        1.2 tken只应在当前网站打开期间有效，所以将token保存在sessionStorage中
                    2. 通过编程式导航跳转到后台主页，路由地址是 /home
                */
               // 将token保存在sessionStorage中
               window.sessionStorage.setItem("token",res.data.token);
               // 跳转到后台主页，路由地址是 /home
               this.$router.push("/home")
            })
        }
    }
}
</script>
<style lang="less" scoped>
    #login{
        height: 100%;
        background-color: #2b4b6b;
    }
    .login_box{
        width: 450px;
        height: 300px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
    }
    .login_admin_head{
        width: 130px;
        height: 130px;
        background-color: #fff;
        border: 1px solid #eee;
        border-radius: 50%;
        box-shadow: 0 0 10px #ddd;
        padding: 10px;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
    }
    .login_admin_head img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee ;
    }
    .btns{
        // float: right;
        display:flex; // 弹性盒模型
        justify-content: flex-end; // 尾部对齐

    }
    .login_form{
        width: 100%;
        padding: 0 20px;
        box-sizing:border-box;
        position: absolute;
        bottom: 0;
    }
</style>